# -*- coding: utf-8 -*-
import os
import sys
import time
from threading import Thread
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtCore import QThread, Qt
from PyQt5.QtGui import QTextCharFormat, QTextOption, QTextCursor
from PyQt5.QtWidgets import QApplication
from airtest.core.api import *
# from Android.upgrade import Upgrade
from common.connect import ConnectUtil
from common.log_util import right, warning
from common.yamlUtil import YamlUtil
from page.android import AndroidUtil
from page.ios import IosUtil
from poco.drivers.ios import iosPoco


class OtaUI:

    file_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) + "\\" + "logs" + "\\" + "android_log.txt"
    a = AndroidUtil()
    i = IosUtil()
    c = ConnectUtil()

    def __init__(self):
        # 从文件中加载UI定义
        self.ui = uic.loadUi("View/OTA.ui")
        # clear
        self.ui.clear.clicked.connect(self.clear_log)
        # 连接安卓
        self.ui.connect_android.clicked.connect(self.is_android)
        # 连接ios
        self.ui.connect_ios.clicked.connect(self.is_ios)

        # 读取日志路径
        self.log_file_path = self.file_path
        self.last_modified_time = os.path.getmtime(self.log_file_path)

        # 开启一个打印日志的线程
        self.log_reader_thread = Thread(target=self.print_message)
        self.log_reader_thread.start()

    # 清屏
    def clear_log(self):
        self.ui.log.clear()

    # 关闭窗口
    def closeEvent(self, event):
        # 例如，停止线程、保存设置等
        self.log_reader_thread.is_alive()
        event.accept()

    # 连接安卓
    def is_android(self):
        # 连接安卓
        self.c.connect_android()
        # 中断升级
        self.ui.kill_ota.clicked.connect(self.a.kill_ota)
        # 取消升级
        self.ui.cancel_ota.clicked.connect(self.a.cancel_ota)
        # 循环升级
        self.ui.cycle_upgrade.clicked.connect(self.a.cycle_upgrade)
        # 升降级
        self.ui.promotion_demotion.clicked.connect(self.a.promotion_demotion)

    def is_ios(self):
        self.i.connect_ios()
        # 中断升级
        self.ui.kill_ota.clicked.connect(self.i.kill_ota)
        # 取消升级
        self.ui.cancel_ota.clicked.connect(self.i.cancel_ota)
        # 循环升级
        self.ui.cycle_upgrade.clicked.connect(self.i.cycle_upgrade)
        # 升降级
        self.ui.promotion_demotion.clicked.connect(self.i.promotion_demotion)

    # 日志打印
    def print_message(self):
        # def run_log_reader():
        while True:
            current_modified_time = os.path.getmtime(self.log_file_path)
            if current_modified_time != self.last_modified_time:
                with open(self.file_path, 'r', encoding='utf-8') as log_file:
                    log_content = log_file.read()
                    log_level = self.extract_log_level(log_content)
                    self.create_char_format(log_level, log_content)

                self.last_modified_time = current_modified_time
            time.sleep(1)  # 每秒更新一次

    def extract_log_level(self, log_content):
        # 从日志内容中提取日志级别
        if "[DEBUG]" in log_content:
            return "DEBUG"
        elif "[INFO]" in log_content:
            return "INFO"
        elif "[WARNING]" in log_content:
            return "WARNING"
        elif "[ERROR]" in log_content:
            return "ERROR"
        elif "[CRITICAL]" in log_content:
            return "CRITICAL"
        else:
            return "UNKNOWN"

    def create_char_format(self, log_level, log_content):
        char_format = QTextCharFormat()
        # 根据日志级别设置颜色
        if log_level == "DEBUG":
            char_format.setForeground(Qt.blue)
        elif log_level == "INFO":
            char_format.setForeground(Qt.darkGreen)
        elif log_level == "WARNING":
            char_format.setForeground(Qt.red)
        elif log_level == "ERROR" or log_level == "CRITICAL":
            char_format.setForeground(Qt.yellow)
        self.ui.log.setCurrentCharFormat(char_format)
        self.ui.log.insertPlainText(log_content)
        self.ui.log.ensureCursorVisible()





